package com.fxwater.websocket.job;

import com.fxwater.websocket.service.IScheduledSqlInfoService;
import com.fxwater.websocket.util.SpringBeanFinder;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.concurrent.*;

/**
 * @Description: 完全基于无参SQL的redis缓存数据生成调度类
 * @Author: 王明朝
 * @Date: 2019/8/17 12:58
 */
public class ScheduledSqlExecuteJob implements Job {


    private final long DELAY = 5000;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        ThreadPoolExecutor executor =new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
        executor.execute(new Runnable() {
            @Override
            public void run() {
                IScheduledSqlInfoService scheduledSqlInfoService = SpringBeanFinder.findBean(IScheduledSqlInfoService.class);
                while(scheduledSqlInfoService==null){
                    try {
                        Thread.sleep(DELAY);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    scheduledSqlInfoService = SpringBeanFinder.findBean(IScheduledSqlInfoService.class);
                }
                if(scheduledSqlInfoService!=null){
                    scheduledSqlInfoService.executeAllSqlAndStore();
                }
            }
        });


    }
}
